package com.wish.building.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.building.entity.*;
import com.wish.building.enums.BuildingType;
import com.wish.building.enums.CommunityType;
import com.wish.building.enums.PremisesType;
import com.wish.building.service.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼盘信息表 前端控制器
 * </p>
 *
 * @author 刘郑欣
 * @since 2022-03-27
 */
@RestController
public class PremisesInfoController {
    //地铁
    @Autowired
    ISubwayService subwayService;
    //商圈
    @Autowired
    IBusinessCircleService businessCircleService;
    //城市
    @Autowired
    IRegionService regionService;
    //楼盘特色
    @Autowired
    IConfigPremisesPropertyService configPremisesPropertyService;
    //楼盘
    @Autowired
    IPremisesInfoService premisesInfoService;
    //周边及交通
    @Autowired
    IPremisesFacilityService premisesFacilityService;
    //特色
    @Autowired
    IRelationPremisesPropertyService relationPremisesPropertyService;
    //图片中间表
    @Autowired
    IAlbumService albumService;

    /*建筑类型*/
    @GetMapping("jianzhuleixing")
    public List<Map<String,Object>> jianzhuleixing() {
        List<Map<String,Object>> list = new ArrayList<>();
        for (BuildingType c : BuildingType.values()) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",c.getIndex());
            map.put("name", c.getName());
            list.add(map);
        }
        return list;
    }

    /*社区类型*/
    @GetMapping("shequleixing")
    public List<Map<String, Object>> shequleixing() {
        List<Map<String,Object>> list = new ArrayList<>();
        for (CommunityType c : CommunityType.values()) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",c.getIndex());
            map.put("name", c.getName());
            list.add(map);
        }
        return list;
    }

    /*物业类型*/
    @GetMapping("wuyeleixing")
    public List<Map<String, Object>> wuyeleixing() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (PremisesType c : PremisesType.values()) {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("id",c.getIndex());
            map.put("name", c.getName());
            list.add(map);
        }
        return list;
    }


    /*地铁*/
    @GetMapping("ditie")
    public List<Subway> ditie(Integer parentId){
        QueryWrapper<Subway> wrapper = new QueryWrapper<>();
        wrapper.eq("city",parentId);
        List<Subway> list = subwayService.list(wrapper);
        return list;
    }


    /*商圈*/
    @GetMapping("shangquan")
    public List<BusinessCircle> shangquan(Integer parentId){
        QueryWrapper<BusinessCircle> wrapper = new QueryWrapper<>();
        wrapper.eq("code",parentId);
        List<BusinessCircle> list = businessCircleService.list(wrapper);
        return list;
    }

    /*城市*/
    @GetMapping("chengshi")
    public List<Region> chengshi(Integer parentId){
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",parentId);
        List<Region> list = regionService.list(wrapper);
        return list;
    }

    /*楼盘特色*/
    @GetMapping("loupantese")
    public List<ConfigPremisesProperty> loupantese(){
        List<ConfigPremisesProperty> list = configPremisesPropertyService.list();
        for (ConfigPremisesProperty configPremisesProperty : list) {
            System.out.println(configPremisesProperty);
        }
        return list;
    }


    /*添加*/
    @PostMapping("save")
    public void save(@RequestBody PremisesInfo premisesInfo){
        QueryWrapper<PremisesInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("address",premisesInfo.getAddress());
        PremisesInfo one = premisesInfoService.getOne(wrapper);
        if(one!=null){
            return;
        }
        //地铁
        Integer[] subwayList1 = premisesInfo.getSubwayList1();
        String s = "";
        for (Integer integer : subwayList1) {
            s += integer+",";
        }
        premisesInfo.setSubway(s);
        //建筑类型
        Integer[] buildingList = premisesInfo.getBuildingList();
        String s1 = "";
        for (Integer integer : buildingList) {
            s1 += integer+",";
        }
        premisesInfo.setBuildingType(s1);
        //商圈
        Integer[] areaList = premisesInfo.getCommercialAreaList();
        String s2 = "";
        for (Integer integer : areaList) {
            s2 += integer+",";
        }
        premisesInfo.setCommercialArea(s2);
        //物业
        Integer[] communityList = premisesInfo.getCommunityList();
        String s3 = "";
        for (Integer integer : communityList) {
            s3 += integer+",";
        }
        premisesInfo.setCommunityType(s3);
        System.out.println("地铁:"+s+"建筑类型:"+s1+"商圈:"+s2+"物业:"+s3);
        boolean save = premisesInfoService.save(premisesInfo);
        if(save){
            //周边配套
            List<PremisesFacility> facilities = premisesInfo.getPeripheralFacilities();
            for (PremisesFacility facility : facilities) {
                facility.setPremisesId(premisesInfo.getId());
                premisesFacilityService.save(facility);
            }
            //交通情况
            List<PremisesFacility> trafficCondition = premisesInfo.getTrafficCondition();
            for (PremisesFacility premisesFacility : trafficCondition) {
                premisesFacility.setPremisesId(premisesInfo.getId());
                premisesFacilityService.save(premisesFacility);
            }
            //特色
            Integer[] propertyFeaturesList = premisesInfo.getPropertyFeaturesList();
            for (Integer integer : propertyFeaturesList) {
                RelationPremisesProperty relationPremisesProperty = new RelationPremisesProperty();
                relationPremisesProperty.setPremisesId(premisesInfo.getId());
                relationPremisesProperty.setPropertyId(integer);
                relationPremisesPropertyService.save(relationPremisesProperty);
            }
            //图片添加
            Album album = new Album();
            //户型图
            if(premisesInfo.getHxtList()!=null){
                List<Albumm> list = premisesInfo.getHxtList();
                for (Albumm albumm : list) {
                    album = new Album();
                    album.setPremisesId(premisesInfo.getId());
                    album.setAlbumType(1);
                    album.setName(albumm.getName());
                    album.setUrl(albumm.getUrl());
                    albumService.save(album);
                }
            }
            //外景图
            if(premisesInfo.getWjtList()!=null){
                for (Albumm albumm : premisesInfo.getWjtList()) {
                    album = new Album();
                    album.setPremisesId(premisesInfo.getId());
                    album.setAlbumType(2);
                    album.setName(albumm.getName());
                    album.setUrl(albumm.getUrl());
                    albumService.save(album);
                }
            }
            //施工图
            if(premisesInfo.getSgtList()!=null){
                for (Albumm albumm : premisesInfo.getSgtList()) {
                    album = new Album();
                    album.setPremisesId(premisesInfo.getId());
                    album.setAlbumType(3);
                    album.setName(albumm.getName());
                    album.setUrl(albumm.getUrl());
                    albumService.save(album);
                }
            }
            //效果图
            if(premisesInfo.getXgtList()!=null){
                for (Albumm albumm : premisesInfo.getXgtList()) {
                    album = new Album();
                    album.setPremisesId(premisesInfo.getId());
                    album.setAlbumType(4);
                    album.setName(albumm.getName());
                    album.setUrl(albumm.getUrl());
                    albumService.save(album);
                }
            }
            //首页图
            if(premisesInfo.getSytList()!=null){
                for (Albumm albumm : premisesInfo.getSytList()) {
                    album = new Album();
                    album.setPremisesId(premisesInfo.getId());
                    album.setAlbumType(5);
                    album.setName(albumm.getName());
                    album.setUrl(albumm.getUrl());
                    albumService.save(album);
                }
            }
        }
        System.out.println(premisesInfo);
    }
}
